﻿using System;
using Besture.Bll;
using Besture.Common;
using Besture.UI.Admin.Master;
using Besture.UI.ClassHelper;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.Data;

namespace Besture.UI.Admin.CommonModel
{
    public partial class FieldList: BasePage
    {
        /// <summary>
        /// 内容模型ID
        /// </summary>
        public string _ModelID = string.Empty;

        #region 页面加载

        /// <summary>
        /// 初始化页面
        /// </summary>
        protected override void InitialLoad()
        {
            _ModelID = Query("ModelID");

            ViewState["ModelID"] = _ModelID;

            BindData();
            AspxGridViewHelper.InitAspxGridView(grid);

            ((ConfigAdmin)this.Master).Title = "内容模型管理";
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {
            BindData();
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        public void BindData()
        {
            string sql = string.Format(@"SELECT tmf.*,td.Name as FieldTypeName FROM TBContentModelField tmf
                            LEFT JOIN TBDictionary td
                            ON tmf.FieldType = td.Value 
                            WHERE td.FlagType = 1 AND tmf.ModelID = {0}", ViewState["ModelID"]);
            grid.DataSource = QueryDac.GetTable(sql);
            grid.DataBind();
        }

        /// <summary>
        /// AspxGridView回发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grid_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            if (e.Parameters == "Refresh")
                grid.DataBind();
        }

        #endregion

        #region 删除数据

        /// <summary>
        /// 删除单条数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grid_RowDeleting(object sender, ASPxDataDeletingEventArgs e)
        {
            e.Cancel = true;
            int ID = Convert.ToInt32(e.Keys[0]);
            ContentModelFieldDac ContentModelFieldDac = new ContentModelFieldDac();
            int iTmp = ContentModelFieldDac.DeleteField(ID);
            BindData();
        }

        #endregion

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string url = "FieldAdd.aspx?ModelID=" + ViewState["ModelID"];
            Response.Redirect(url);

            
        }
    }
}